package cn.cdzdjd.constant;

import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;

import cn.cdzdjd.exception.ErrorCode;
import cn.cdzdjd.exception.RenException;

public class LocationRedirect {
	/**
	 * 移动端获取跳转后的链接
	 * 
	 * @param url
	 * @return
	 */
	public static String getLocationMobile(String url) {
		try {
			Response response = Jsoup.connect(url).ignoreContentType(true).followRedirects(false)
					.userAgent(BrowserConstant.USER_AGENT_MOBILE).header("Accept", BrowserConstant.ACCEPT).referrer(url)
					.timeout(3000).execute();
			return response.header("Location") != null ? response.header("Location") : url;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RenException(ErrorCode.PARSE_ERROR);
		}
	}

	/**
	 * PC端获取跳转后的链接
	 * 
	 * @param url
	 * @return
	 */
	public static String getLocationPC(String url) {
		try {
			Response response = Jsoup.connect(url).ignoreContentType(true).followRedirects(false)
					.userAgent(BrowserConstant.USER_AGENT_PC).header("Accept", BrowserConstant.ACCEPT).referrer(url)
					.timeout(3000).execute();
			return response.header("Location") != null ? response.header("Location") : url;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RenException(ErrorCode.PARSE_ERROR);
		}
	}
}
